package xine.demo.ssm.controller;

import com.sun.javafx.collections.MappingChange;

import xine.demo.ssm.constant.ViewContant;
import xine.demo.ssm.view.SingleExcelView;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class TestController {

    //方法形参，引用数据类型的参数可以没有
    @RequestMapping("/getValue")
    public String getValue(String name, Integer age, String test){

        StringBuilder message = new StringBuilder("接收到请求：");
        if(!StringUtils.isEmpty(name)){
            message.append("name =").append(name);
        }

        if(!StringUtils.isEmpty(age)){
            message.append(" age = ").append(age);
        }

        if(!StringUtils.isEmpty(test)){
            message.append(" test = ").append(test);
        }

        System.out.println(message);

        return "index";
    }

    @RequestMapping("/json")
    @ResponseBody
    public Map<String,Object> getObject(){
        System.out.println("接受到请求");
        Map<String,Object> map = new HashMap<>();
        map.put("name","Li Ming");
        map.put("age",18);
        map.put("is_man",false);

        return map;
    }

    @RequestMapping(value="/upload1",method = RequestMethod.POST)
    public String uploadFile(@RequestParam("myFile")  MultipartFile file){
        String originalFileName = file.getOriginalFilename();
        System.out.println(originalFileName);

//        try {
//            File destFile = new File("uploads/" + originalFileName);//加了/变成了磁盘顶级目录,否则会项目内,目录不会自动创建
//            System.out.println(destFile.getAbsolutePath());
//            file.transferTo(destFile); //用于保存o为永久文件,临时文件会在本方法结束后被删除
//       } catch (IOException e) {
//            e.printStackTrace();
//        }
        if(file.getSize() > 1024*1024 *4){
            System.out.println("文件过大");
        }

        //System.out.println(file.getName());

        return "index";
    }

    @RequestMapping("/export")
    public ModelAndView exportExcel(String condition, HttpServletRequest request, HttpServletResponse response) {
        try{
            //准备数据
            Map<String, Object> model = new HashMap<String, Object>();
            model.put("total", 12);
            model.put("filename", "资产信息-" + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date())+".xls");
            model.put("title", "sheet名");

            List<Map<String,Object>> list = new ArrayList<>();
            for(int i = 0; i < 10; i++){
                Map<String, Object> map = new HashMap<>();
                map.put("name","test" + i);
                map.put("age",i);
                map.put("is_boy",true);
                list.add(map);
            }
            model.put(SingleExcelView.MAP_LIST,list);

            return new ModelAndView(ViewContant.VIEW_EXCEL,model);
        } catch (Throwable e) {
            //throw new RuntimeException(e);
        }
        return null;
    }

    @RequestMapping(value = "/import",method = RequestMethod.POST)
    public ModelAndView importExcel(@RequestParam("myFile")  MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        try{
            //new BufferedReader() file.getInputStream();
            WorkbookFactory.create(file.getInputStream());



//            return new ModelAndView(ViewContant.VIEW_EXCEL,model);
        } catch (Throwable e) {
            //throw new RuntimeException(e);
        }
        return null;
    }

}
